<script>
	export default {
		onLaunch: function() {
			// #ifdef APP-PLUS 
			this.unpdateApp()
			this.uniPush()
			// #endif
			// #ifdef MP-WEIXIN
			// this.getBounding()
			this.updateManagerUpgrade()
			// #endif
			this.userAppid()
			console.log('App Launch')
		},
		onShow: function() {
			// console.log('App Show')
			// #ifdef APP-PLUS
			this.claerBadge()
			// #endif
		},
		onHide: function() {
			console.log('App Hide')
		},
		globalData: {
			systemMain: {}, // 微信系统高度
			typeScode: '',
			typeState: '',
			pushCid: '',
			androidIos: '',
			source: '',
			weiverName: '',
			cid:''//客户端标识

		},
		methods: {
			// 用于获取微信三个点的位置
			// console.log(menuButtonInfo,'666666666666666')
			uniPush() {
				var pinf = plus.push.getClientInfo();
				var cid = pinf.clientid; //客户端标识
				this.globalData.cid = cid
				// console.log(this.globalData.cid,'客户端标识');
				plus.push.addEventListener(
					'click',
					function(msg) {
						uni.navigateTo({
							url: 'pages/message/index'
						});
					},
					false
				);
				plus.push.addEventListener(
					'receive',
					function(msg) {
						uni.navigateTo({
							url: 'pages/message/index'
						});
					},
					false
				);
			},
			getBounding() {
				let menuButtonInfo = uni.getMenuButtonBoundingClientRect()
				this.globalData.systemMain = menuButtonInfo
			},
			// #ifdef APP-PLUS
			claerBadge() {
				if (plus.os.name.toLowerCase() == 'ios') {
					//导入ios UIApplication  
					var UIApplication = plus.ios.import("UIApplication");
					var app = UIApplication.sharedApplication();
					//获取应用图标的数量  
					// var oldNum = app.applicationIconBadgeNumber();  
					// var newNum = oldNum - 1;  
					//设置应用图标的数量  
					plus.runtime.setBadgeNumber(0);
					//导入个推原生类  
					var GeTuiSdk = plus.ios.importClass('GeTuiSdk');
					GeTuiSdk.setBadge(0);
				}
			},
			// #endif

			userAppid() {
				var androidIos
				var source
				if (uni.getSystemInfoSync().platform == 'android') {
					source = 1
					// #ifdef APP-PLUS 
					androidIos = '8da58a92-b41f-46d6-9cd9-7983723bb5a5 '
					// #endif
					// #ifdef MP-WEIXIN
					androidIos = '05a1fc14-aa4f-4604-a590-03570287f182 '
					// #endif
				} else {
					source = 2
					// #ifdef APP-PLUS
					androidIos = 'ac0cc767-0abe-4600-a6f5-703e21495d91 '
					// #endif
					// #ifdef MP-WEIXIN
					androidIos = '05a1fc14-aa4f-4604-a590-03570287f182 '
					// #endif
				}

				// #ifdef H5
				source = 1
				androidIos = '8da58a92-b41f-46d6-9cd9-7983723bb5a5'
				// #endif
				this.globalData.androidIos = androidIos
				this.globalData.source = source
			},
			// 判断有无token 自动登录
			// configToken() {
			// 	let tokenCode = uni.getStorageSync('token')
			// 	// 有token登录信息的时候
			// 	if (tokenCode) {
			// 		// #ifdef H5
			// 		this.Refresh()
			// 		// #endif
			// 		// #ifndef H5
			// 		uni.switchTab({
			// 			url: '/pages/index/index'
			// 		})
			// 		// #endif
			// 	} else {
			// 		uni.redirectTo({
			// 			url: '/pages/login/login'
			// 		})
			// 	}
			// },
			// 版本升级
			// #ifdef MP-WEIXIN
			updateManagerUpgrade() {
				// 检查当前版本是否可用
				if (uni.canIUse('getUpdateManager')) {
					const updateManager = uni.getUpdateManager();
					updateManager.onCheckForUpdate(function(res) {
						// res.hasUpdate == true 有新版本
						if (res.hasUpdate) {
							updateManager.onUpdateReady(function(res) {
								uni.showModal({
									title: '更新提示',
									content: '新版本已经准备好，是否重启应用？',
									success(res) {
										if (res.confirm) {
											// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
											updateManager.applyUpdate();
										}
									}
								});
							});
						}
						// 新版本下载失败时执行
						updateManager.onUpdateFailed(function() {
							wx.showModal({
								title: '发现新版本',
								content: '请删除当前小程序，重新搜索打开...',
							})
						})
					});
				} else {}
			},
			// #endif
			// 刷新判断,
			// #ifdef H5
			Refresh() {
				var route = this.$router // 获取路由实例
				var routelist = route.options.routes // 获取路由数组
				if (!Array.isArray(routelist)) return
				var fullPath = route.currentRoute // 当前路由信息
				var path = routelist.find(function(item) {
					return item.path == fullPath.path
				}) // 判断当前是否在taber上
				if (typeof path.meta.tabBarIndex == 'number') { /// 跳转到taber上
					uni.switchTab({
						url: fullPath.fullPath
					})
				} else {
					var loginlist = ['/', '/pages/login/mobile_login', '/pages/login/codeLogin'] // 不需要跳转的元素
					var loginPath = loginlist.findIndex(function(item) {
						return item == fullPath.path
					})
					// console.log(loginPath)
					if (loginPath != '-1') {
						uni.switchTab({
							url: '/pages/index/index',
						})
					} else {
						uni.navigateTo({
							url: fullPath.fullPath
						})
					}
				}
			},
			// #endif
			unpdateApp() {
				let version = uni.getStorageSync('app-version');
				this.$http.get('api/Home/GetAppVersion').then(res => {
					// console.log(res.versionname, '小程序455555555');
					let weiVerName = res.versionname
					this.globalData.weiverName = weiVerName
					// #ifdef APP-PLUS 
					plus.runtime.getProperty(plus.runtime.appid, (wgtinfo) => {
						// console.log(wgtinfo.version, res.versionname, 99999999999999, version);
						let server = res.versionname.replace('.', '').replace('.', '')
						let local = wgtinfo.version.replace('.', '').replace('.', '')

						if (res.versionname == wgtinfo.version) {
							uni.setStorageSync('app-version', res.versionname)
						} else if (server > local) {
							uni.navigateTo({
								url: `/pages/modules/update?code=${res.remarks}`,
							})
						} else {
							uni.setStorageSync('app-version', wgtinfo.version)
						}
					})
					// #endif
				})
			}
		}
	}
</script>

<style lang="scss">
	/*每个页面公共css */
	@import '@/uni_modules/uni-scss/index.scss';
	/* #ifndef APP-NVUE */
	@import '@/static/customicons.css';

	// 设置整个项目的背景色
	page {
		background-color: #f5f5f5;
	}

	/* #endif */
	.example-info {
		font-size: 14px;
		color: #333;
		padding: 10px;
	}

	//设置圆角
	checkbox.round .wx-checkbox-input,
	checkbox.round .uni-checkbox-input {
		border-radius: 100upx;
	}

	//设置背景色
	checkbox.red[checked] .wx-checkbox-input,
	checkbox.red.checked .uni-checkbox-input {
		background-color: #B4001E !important;
		border-color: #e54d42 !important;
		color: #ffffff !important;
	}

	.allfont .uni-list-item__content-title {
		font-weight: bold;
		font-size: 15px !important;
	}

	// .uni-searchbar__box-search-input {
	// 	color: #333 !important;
	// }

	#img-preview-video {
		width: 90vw !important;
		height: 90vh !important;

	}
</style>